//
//  InviteViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "InviteViewController.h"

#import "ShareView.h"
#import "UIImage+GradientColor.h"

#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "WeiboSDK.h"
#import "WXApi.h"

@interface InviteViewController ()<TencentSessionDelegate>

@property (nonatomic, strong) UIButton *shadowBtn;
@property (nonatomic, strong) ShareView *shareView;
@property (nonatomic, copy) NSString *proId;
@property (nonatomic, copy) NSString *shareId;
@property (nonatomic, copy) NSString *shareName;
@property (nonatomic, copy) NSString *shareLink;

@property (nonatomic, strong) TencentOAuth *tencentOAuth;

@end

@implementation InviteViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.shadowBtn removeFromSuperview];
    [self.shareView removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"邀请好友";
    
    [self setUI];
}

- (void)setUI {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, ScreenX375(606))];
    imageView.image = ImageWithName(@"邀请有礼");
    [self.view addSubview:imageView];
    
    UIView *inviteView = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(305), SCREEN_WIDTH - ScreenX375(32), ScreenX375(90))];
    inviteView.backgroundColor = [UIColor whiteColor];
    inviteView.layer.cornerRadius = ScreenX375(5);
    inviteView.clipsToBounds = YES;
    [self.view addSubview:inviteView];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(66), ScreenX375(30), SCREEN_WIDTH - ScreenX375(32) - ScreenX375(132), 0.5)];
    line.backgroundColor = REXADECIMALCOLOR(@"#d8d8d8");
    [inviteView addSubview:line];
    
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(112)) / 2, ScreenX375(20), ScreenX375(80), ScreenX375(21))];
    noteLabel.text = @"我的邀请码";
    noteLabel.font = KBlodfont(15);
    noteLabel.backgroundColor = [UIColor whiteColor];
    noteLabel.textAlignment = NSTextAlignmentCenter;
    noteLabel.textColor = REXADECIMALCOLOR(@"#2e2e2e");
    [inviteView addSubview:noteLabel];
    
    UILabel *inviteCode = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(53), SCREEN_WIDTH - ScreenX375(32), ScreenX375(21))];
    inviteCode.text = kNSUDefaultReadKey(@"inviteCode");
    inviteCode.textAlignment = NSTextAlignmentCenter;
    inviteCode.font = KBlodfont(15);
    inviteCode.textColor = REXADECIMALCOLOR(@"#2e2e2e");
    [inviteView addSubview:inviteCode];
    
    UIImage *bgImg = [UIImage gradientColorImageFromColors:@[REXADECIMALCOLOR(@"#fd4d4d"),REXADECIMALCOLOR(@"#ff1062")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(ScreenX375(157), ScreenX375(42))];
    UIButton *shareBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(157)) / 2, ScreenX375(445), ScreenX375(157), ScreenX375(42))];
    [shareBtn setTitle:@"分享好友" forState:UIControlStateNormal];
    shareBtn.titleLabel.font = KBlodfont(15);
    [shareBtn setTitleColor:REXADECIMALCOLOR(@"#ffffff") forState:UIControlStateNormal];
    shareBtn.layer.cornerRadius = ScreenX375(21);
    shareBtn.clipsToBounds = YES;
    [shareBtn setBackgroundImage:bgImg forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareBtn];
}

- (void)share {
    if (!self.shareView) {
        self.shadowBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.shadowBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self.shadowBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        self.shadowBtn.hidden = YES;
        [kKeyWindow addSubview:self.shadowBtn];
        
        self.shareView = [[ShareView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, ScreenX375(150))];
        __weak __typeof(self) weakSelf = self;
        self.shareView.cancel = ^{
            [weakSelf hidden];
        };
        self.shareView.wxClick = ^{
            SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
            sendReq.bText = NO;
            sendReq.scene = 0;
            
            // 2.创建分享内容
            WXMediaMessage *message = [WXMediaMessage message];
            //分享标题
            message.title = @"虾拼";
            // 描述
            message.description = weakSelf.shareName;
            //分享图片,使用SDK的setThumbImage方法可压缩图片大小
            //                message.thumbData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEHEADER,self.model.pics.firstObject]]];
            [message setThumbImage:[UIImage imageNamed:@"虾拼 logo"]];
            
            //创建多媒体对象
            WXWebpageObject *webObj = [WXWebpageObject object];
            // 点击后的跳转链接
            webObj.webpageUrl = weakSelf.shareLink;
            message.mediaObject = webObj;
            sendReq.message = message;
            [WXApi sendReq:sendReq];
            [weakSelf hidden];
        };
        self.shareView.wxGroupClick = ^{
            SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
            sendReq.bText = NO;
            sendReq.scene = 1;
            
            // 2.创建分享内容
            WXMediaMessage *message = [WXMediaMessage message];
            //分享标题
            message.title = @"虾拼";
            // 描述
            message.description = weakSelf.shareName;
            //分享图片,使用SDK的setThumbImage方法可压缩图片大小
            [message setThumbImage:[UIImage imageNamed:@"虾拼 logo"]];
            
            //创建多媒体对象
            WXWebpageObject *webObj = [WXWebpageObject object];
            // 点击后的跳转链接
            webObj.webpageUrl = weakSelf.shareLink;
            message.mediaObject = webObj;
            sendReq.message = message;
            [WXApi sendReq:sendReq];
            [weakSelf hidden];
        };
        self.shareView.qqClick = ^{
            weakSelf.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppKey andDelegate:weakSelf];
            
            NSString *url = weakSelf.shareLink;
            NSString *imageUrl = [NSString stringWithFormat:@"%@",IMAGEHEADER];
            
            QQApiNewsObject *share = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:@"虾拼" description:weakSelf.shareName previewImageURL:[NSURL URLWithString:imageUrl]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:share];
            
            //分享到qq
            [QQApiInterface sendReq:req];
            [weakSelf hidden];
        };
        self.shareView.qqGroupClick = ^{
            weakSelf.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppKey andDelegate:weakSelf];
            
            NSString *url = weakSelf.shareLink;
            NSString *imageUrl = [NSString stringWithFormat:@"%@",IMAGEHEADER];
            
            QQApiNewsObject *share = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:@"虾拼" description:weakSelf.shareName previewImageURL:[NSURL URLWithString:imageUrl]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:share];
            
            //分享到qq
            [QQApiInterface SendReqToQZone:req];
            [weakSelf hidden];
        };
        self.shareView.wbClick = ^{
            //微博
            WBMessageObject *message = [WBMessageObject message];
            message.text = [NSString stringWithFormat:@"虾拼 %@  %@",weakSelf.shareName,weakSelf.shareLink];
            
            WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
            authRequest.redirectURI = kWeiboAppRedirectURL;
            authRequest.scope = @"all";
            
            WBSendMessageToWeiboRequest *sharerequest = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:nil];
            [WeiboSDK sendRequest:sharerequest];
            [weakSelf hidden];
        };
        [kKeyWindow addSubview:self.shareView];
    }
    self.shadowBtn.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT - ScreenX375(150), SCREEN_WIDTH, ScreenX375(150));
    }];
}

- (void)hidden {
    [UIView animateWithDuration:0.3 animations:^{
        self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ScreenX375(180));
    } completion:^(BOOL finished) {
        self.shadowBtn.hidden = YES;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
